package com.lianwei.hospital.Service.Impl;

import com.lianwei.hospital.Service.DoctorService;
import com.lianwei.hospital.mapper.DoctorInfoMapper;
import com.lianwei.hospital.module.entity.Admain;
import com.lianwei.hospital.module.entity.DoctorInfo;
import com.lianwei.hospital.module.vo.DoctorVO;
import com.lianwei.hospital.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service("DoctorService")
public class DoctorInfoImpl implements DoctorService {
    @Autowired
    DoctorInfoMapper doctorInfoMapper;

    @Override
    public DoctorInfo selectByPrimaryKey(String id) throws Exception {
        return doctorInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean insert(DoctorInfo doctorInfo) throws Exception {
        return doctorInfoMapper.insert(doctorInfo)>0;
    }

    @Override
    public List<DoctorInfo> selectAll() throws Exception {
        return doctorInfoMapper.selectAll();
    }

    @Override
    public boolean deleteByPrimaryKey(String id) throws Exception {
        return doctorInfoMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public boolean updateByPrimaryKey(DoctorInfo doctorInfo) throws Exception {
        return doctorInfoMapper.updateByPrimaryKey(doctorInfo)>0;
    }

    @Override
    public PageUtil<DoctorInfo> findPage(DoctorVO query) throws Exception {
        /*
         * 1.获取总记录数
         * 2.如果总记录数 > 0 查询数据
         *  将数据封装到PageUtil
         * 否则，返回null
         */
        int count=doctorInfoMapper.countBySelectActive(query);
        if (count>0){
            List<DoctorInfo> data=doctorInfoMapper.selectListBySelectActive(query);
            PageUtil<DoctorInfo> pageUtil=new PageUtil<>(query.getCurrentPage(),
                    query.getPageSize(),count,data);
            return pageUtil;
        }
        return null;
    }

    @Override
    public DoctorInfo login(String id, String password) throws Exception {
        DoctorInfo doctorInfo= doctorInfoMapper.findByUsername(id);
        if (doctorInfo == null){
            throw new Exception("用户未注册");
        }else {
            if (doctorInfo.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
                return doctorInfo;
            }else {
                throw new Exception("用户名密码错误");
            }
        }
    }
}
